package com.toune.dltools.adapter

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.Gravity
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.*
import androidx.core.view.get
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.tamsiree.rxkit.view.RxToast
import com.toune.dltools.R
import com.toune.dltools.bean.DLCreateFormBean
import com.toune.dltools.widget.DLInputEditTextView
import com.toune.dltools.widget.LineWrapRadioGroup
import kotlinx.android.synthetic.main.layout_input_edit_text_view.view.*
import java.lang.StringBuilder

class DLFormAdapter :
    BaseMultiItemQuickAdapter<DLCreateFormBean, BaseViewHolder> {
    constructor(data: MutableList<DLCreateFormBean>?) : super(data) {
        addItemType(DLCreateFormBean.SINGLE_INPUT, R.layout.layout_single_input_form)
        addItemType(DLCreateFormBean.MUL_INPUT, R.layout.layout_single_input_form)
        addItemType(DLCreateFormBean.PSW_INPUT, R.layout.layout_single_input_form)
        addItemType(DLCreateFormBean.SPINNER, R.layout.layout_spinner_form)
        addItemType(DLCreateFormBean.TEXT, R.layout.layout_spinner_form)
        addItemType(DLCreateFormBean.SINGLE_CHECK, R.layout.layout_single_check_form)
        addItemType(DLCreateFormBean.MUL_CHECK, R.layout.layout_single_check_form)
        addItemType(DLCreateFormBean.SWITCH, R.layout.layout_single_check_form)
        addItemType(DLCreateFormBean.DATE, R.layout.layout_spinner_form)
    }

    override fun convert(holder: BaseViewHolder, item: DLCreateFormBean) {
        when (item.tag) {
            DLCreateFormBean.SINGLE_INPUT -> {
                holder.setText(R.id.nameTv, item.params[0])
                val dlInputEditTv = holder.getView<DLInputEditTextView>(R.id.inputEt)
                dlInputEditTv.hintStr = item.params[1]
                dlInputEditTv.editText.setLines(1)
            }
            DLCreateFormBean.MUL_INPUT -> {
                holder.setText(R.id.nameTv, item.params[0])
                val nameTv = holder.getView<TextView>(R.id.nameTv)
                nameTv.gravity = Gravity.TOP
                val dlInputEditTv = holder.getView<DLInputEditTextView>(R.id.inputEt)
                dlInputEditTv.hintStr = item.params[1]
                dlInputEditTv.editText.setLines(3)
                dlInputEditTv.editText.gravity = Gravity.TOP and Gravity.LEFT
            }
            DLCreateFormBean.PSW_INPUT -> {
                holder.setText(R.id.nameTv, item.params[0])
                val nameTv = holder.getView<TextView>(R.id.nameTv)
                val dlInputEditTv = holder.getView<DLInputEditTextView>(R.id.inputEt)
                dlInputEditTv.hintStr = item.params[1]
                dlInputEditTv.editText.setLines(1)
                setPasswordEye(dlInputEditTv.editText)
                dlInputEditTv.RIGHT_ICON_STATE = DLInputEditTextView.RIGHT_ICON_ALWAY_VISiBLE
                dlInputEditTv.resourceId = R.drawable.ic_password
                dlInputEditTv.onInputEditTextViewClickListener =
                    object : DLInputEditTextView.OnInputEditTextViewClickListener {
                        override fun rightIconClick(imageView: ImageView) {
                            setPasswordEye(dlInputEditTv.editText)
                        }

                        override fun focusChangeListener(string: String, hasFocus: Boolean) {
                        }

                        override fun searchListener(str: String) {
                        }

                    }
            }
            DLCreateFormBean.SPINNER -> {
                holder.setText(R.id.nameTv, item.params[item.params.lastIndex])
                val nameTv = holder.getView<TextView>(R.id.nameTv)
                val dlInputEditTv = holder.getView<TextView>(R.id.inputEt)
                dlInputEditTv.hint = item.params[item.params.lastIndex - 1]
                dlInputEditTv.setOnClickListener {
                    val bottomListSheetBuilder = QMUIBottomSheet.BottomListSheetBuilder(context)
                    for (s in item.params.subList(0, item.params.lastIndex - 1)) {
                        bottomListSheetBuilder.addItem(s)
                    }
                    bottomListSheetBuilder.setOnSheetItemClickListener { dialog, itemView, position, tag ->
                        RxToast.showToast(tag)
                        dlInputEditTv.text = tag
                        dialog.dismiss()
                    }
                    bottomListSheetBuilder.build().show()
                }
            }

            DLCreateFormBean.TEXT -> {
                holder.setText(R.id.nameTv, item.params[0])
                val nameTv = holder.getView<TextView>(R.id.nameTv)
                val dlInputEditTv = holder.getView<TextView>(R.id.inputEt)
                dlInputEditTv.visibility = View.GONE
            }

            DLCreateFormBean.SINGLE_CHECK -> {
                holder.setText(R.id.nameTv, item.params[item.params.lastIndex])
                val lv = holder.getView<LinearLayout>(R.id.lv)
                val radioGroup = LineWrapRadioGroup(context)
                radioGroup.orientation = RadioGroup.HORIZONTAL
                for ((index, datum) in item.params.subList(0, item.params.lastIndex)
                    .withIndex()) {
                    val radioButton = RadioButton(context)
                    radioButton.text = datum
                    radioButton.tag = datum
                    radioButton.id = index
                    radioGroup.addView(radioButton)
                }
                radioGroup.setOnCheckedChangeListener { group, checkedId ->
                    RxToast.showToast(group[checkedId].tag.toString())
                }
                lv.addView(radioGroup)
            }

            DLCreateFormBean.MUL_CHECK -> {
                val checkBoxs = ArrayList<CheckBox>()
                holder.setText(R.id.nameTv, item.params[item.params.lastIndex])
                val lv = holder.getView<LinearLayout>(R.id.lv)
                val sb = HashMap<Int, String>()
                for ((index, datum) in item.params.subList(0, item.params.lastIndex)
                    .withIndex()) {
                    val checkBox = CheckBox(context)
                    checkBox.text = datum
                    checkBox.tag = datum
                    checkBox.id = index
                    checkBoxs.add(checkBox)
                    checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                        for ((index, checkBox) in checkBoxs.withIndex()) {
                            if (checkBox.isChecked) {
                                sb[index] = checkBox.tag.toString()
                            } else {
                                sb.remove(index)
                            }
                        }
                        RxToast.showToast(sb.toString())
                    }
                    lv.addView(checkBox)
                }
            }

            DLCreateFormBean.SWITCH -> {
                holder.setText(R.id.nameTv, item.params[0])
                val lv = holder.getView<LinearLayout>(R.id.lv)
                val switch = Switch(context)
                switch.setOnCheckedChangeListener { buttonView, isChecked ->
                    RxToast.showToast("$isChecked")
                }
                lv.addView(switch)
            }

            DLCreateFormBean.DATE -> {
                holder.setText(R.id.nameTv, item.params[0])
                val nameTv = holder.getView<TextView>(R.id.nameTv)
                val dlInputEditTv = holder.getView<TextView>(R.id.inputEt)
                dlInputEditTv.hint = item.params[item.params.lastIndex - 1]
                dlInputEditTv.setOnClickListener {
                    val bottomListSheetBuilder = QMUIBottomSheet.BottomListSheetBuilder(context)
                    for (s in item.params.subList(0, item.params.lastIndex - 1)) {
                        bottomListSheetBuilder.addItem(s)
                    }
                    bottomListSheetBuilder.setOnSheetItemClickListener { dialog, itemView, position, tag ->
                        RxToast.showToast(tag)
                        dlInputEditTv.text = tag
                        dialog.dismiss()
                    }
                    bottomListSheetBuilder.build().show()
                }
            }
        }
    }

    //设置密码可见和不可见
    private fun setPasswordEye(editText: EditText) {
        if (EditorInfo.TYPE_TEXT_VARIATION_PASSWORD == editText.getInputType()) {
            //如果不可见就设置为可见
            editText.inputType = EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
        } else {
            //如果可见就设置为不可见
            editText.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
            editText.transformationMethod = PasswordTransformationMethod.getInstance()
        }
        //执行上面的代码后光标会处于输入框的最前方,所以把光标位置挪到文字的最后面
        editText.setSelection(editText.text.toString().length)
    }
}